
package com.jf.cloud.coupon.listener;

import com.jf.cloud.common.order.bo.PayNotifyBO;
import com.jf.cloud.common.order.constant.OrderType;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.common.util.Json;
import com.jf.cloud.coupon.service.CouponLockService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author zz
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_NOTIFY_SERVICE_TOPIC,consumerGroup = RocketMqConstant.ORDER_NOTIFY_COUPON_GROUP)
public class OrderNotifyCouponConsumer implements RocketMQListener<PayNotifyBO> {
    private static final Logger LOG = LoggerFactory.getLogger(OrderNotifyCouponConsumer.class);
    @Autowired
    private CouponLockService couponLockService;


    /**
     * 订单支付成功，将优惠券标记成已使用状态
     * @param message
     */
    @Override
    public void onMessage(PayNotifyBO message) {
        // 普通订单和预售订单才能使用优惠券
        if (!Objects.equals(message.getOrderType(), OrderType.ORDINARY.value())) {
            return;
        }
        // 采购订单不使用优惠券
        if (Objects.equals(message.getPurchaseOrder(), Boolean.TRUE)) {
            return;
        }
        LOG.info("订单支付成功，将优惠券标记成已使用状态... message: " + Json.toJsonString(message));
        couponLockService.markerCouponUse(message.getOrderIds());
    }
}
